#include "entity.h"

#include "map_system.h"

Entity::Entity(MapSystem &map_system, EntityType type)
    : id_(map_system.GetEntityId()),
      status_(kEntityStatusRemove),
      move_compoent_(&map_system, this),
      type_(type),
      map_system_(map_system),
      is_in_map_(true) {}

Entity::~Entity() {}

void Entity::Init(const Vector2D &direction, const ZNumber &scale,
                  const ZNumber &collision, const Vector2D &position) {
  direction_ = direction;
  scale_ = scale;
  position_ = position;
  collision_= collision;

  if (IsInMap()) {
    map_system_.OnEntityIntoMap(shared_from_this());
  }
}

void Entity::SetSpeed(int32_t speed) {
  move_compoent_.SetSpeed(ZNumber::MakeByReal(speed));
}

void Entity::SetDirection(const Vector2D &direction) {
  direction_ = direction;
  // SetPropertyDirty(kEntityPropertyType_Map);
}

void Entity::SetPosition(const Vector2D &position) {
  auto old_pos = position_;

  position_ = position;
  // SetPropertyDirty(kEntityPropertyType_Map);
  if (IsInMap()) {
    map_system_.OnEntityChangePos(shared_from_this(), old_pos, position_);
  }
}

void Entity::SetSacle(const ZNumber &scale) { scale_ = scale; }

void Entity::SetCollision(const ZNumber &collision) { collision_ = collision; }

void Entity::SetIsInMap(bool is_in_map) {
  if (is_in_map_ == is_in_map) {
    return;
  }
  is_in_map_ = is_in_map;
  if (is_in_map_) {
  } else {
  }
}

Vector2D Entity::GetCenterPosition() { 
  Vector2D position = position_;
  map_system_.MapIndexToCenterPos(position.GridX(),
                                 position.GridY(), position);
  return position;
}